package com.yanqu.road.dao.impl.activity.hsisland;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandBoxData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandFloorData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class HsIslandFloorDataDaoImpl extends TempDataObjectDao<HsIslandFloorData> {

    public static String getHsIslandFloorDataSql = "select * from `t_c_activity_hs_island_floor_data` where `activityId`=? and `groupId`=? and `unionId`=?";
    public Map<Integer, HsIslandFloorData> getHsIslandFloorMap(int activityId,int groupId,String unionId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, HsIslandFloorData> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandFloorDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setString(i++, unionId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandFloorData data = parse(rs);
                map.put(data.getFloorId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateHsIslandFloorDataSql = "update `t_c_activity_hs_island_floor_data` set boxIslandId=?,boxFindUserId=?,boxDataMap=?,maxUnEventId=?,createTime=? where activityId=? and groupId=? and unionId=? and floorId=?";
    @Override
    protected boolean updateData(HsIslandFloorData hsIslandFloorData) {
        hsIslandFloorData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getBoxIslandId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandFloorData.getBoxFindUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandFloorData.getBoxDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getMaxUnEventId().get()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandFloorData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandFloorData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getFloorId()));
        if (getDb().executeNoneQuery(updateHsIslandFloorDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandFloorDataSql = "insert into `t_c_activity_hs_island_floor_data`(activityId,groupId,unionId,floorId,boxIslandId,boxFindUserId,boxDataMap,maxUnEventId,createTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandFloorData hsIslandFloorData) {
        hsIslandFloorData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandFloorData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getFloorId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getBoxIslandId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandFloorData.getBoxFindUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hsIslandFloorData.getBoxDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandFloorData.getMaxUnEventId().get()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandFloorData.getCreateTime()));
        if (getDb().executeNoneQuery(insertHsIslandFloorDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandFloorData parseData(ResultSet rs) throws SQLException {
        HsIslandFloorData hsIslandFloorData = new HsIslandFloorData();
        hsIslandFloorData.setActivityId(rs.getInt("activityId"));
        hsIslandFloorData.setGroupId(rs.getInt("groupId"));
        hsIslandFloorData.setUnionId(rs.getString("unionId"));
        hsIslandFloorData.setFloorId(rs.getInt("floorId"));
        hsIslandFloorData.setBoxIslandId(rs.getInt("boxIslandId"));
        hsIslandFloorData.setBoxFindUserId(rs.getLong("boxFindUserId"));
        hsIslandFloorData.setBoxDataMap(JSON.parseObject(rs.getString("boxDataMap"), new TypeReference<Map<String, HsIslandBoxData>>() {}));
        hsIslandFloorData.setMaxUnEventId(new AtomicInteger(rs.getInt("maxUnEventId")));
        hsIslandFloorData.setCreateTime(rs.getLong("createTime"));
        return hsIslandFloorData;
    }
}
